package com.song.composejoke.ui.item

import android.content.Context
import android.content.Intent
import android.widget.Toast
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.Card
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.song.composejoke.SecondActivity

@Composable
fun JokeItem(name: String, index: Int, context: Context) {
    Column(content = {
        Card(
            modifier = Modifier
                .wrapContentHeight()
                .fillMaxWidth()
                .padding(8.dp)
                .clickable(enabled = true, onClick = {
                    Toast
                        .makeText(context, "点击第${index}条笑话", Toast.LENGTH_SHORT)
                        .show()

                    val intent = Intent(context, SecondActivity::class.java)
                    intent.putExtra("name", name)
                    intent.putExtra("index", index)
                    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                    context.startActivity(intent)
                }),
            content = {
                Text(
                    name,
                    modifier = Modifier
                        .wrapContentHeight()
                        .fillMaxWidth()
                        .padding(8.dp),
                    maxLines = 3,
                    fontSize = 17.sp,
                    overflow = TextOverflow.Ellipsis,
                    lineHeight = 28.sp
                )
            },
            backgroundColor = Color.White,
            elevation = 5.dp,
            border = BorderStroke(1.dp, Color.Gray)
        )
    })
}